package wiz.client.activities.finishedgame;

import wiz.client.ClientFactory;
import wiz.client.activities.GameScorePresenter;
import wiz.client.activities.entrypoint.EntryPointPlace;
import wiz.client.model.WizGameModel;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class FinishedGameActivity extends AbstractActivity {

	private ClientFactory fClientFactory;
	private FinishedGameView fView;
	private String fLastGameState;
	private GameScorePresenter fGameScorePresenter;

	public FinishedGameActivity(ClientFactory clientFactory) {
		fClientFactory = clientFactory;
		FinishedGameView view = fClientFactory.buildFinishedGameView();
		fView = view;
		fGameScorePresenter = new GameScorePresenter(clientFactory, fView.getGameScoreView());
		initHandlers();

	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		panel.setWidget(fView);
		Window.scrollTo(0, Window.getClientHeight());

	}

	private void initHandlers() {
		fView.getStartNewGameClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				startNewGameClicked();
			}
		});
		
		fView.getSubmitScoreClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				submitScoreClicked();
				
			}
		});
		
	}

	protected void submitScoreClicked() {
		//TODO: Ensure scores only submitted once per game
		//TODO: Prevent game modification if scores submitted
	}

	protected void startNewGameClicked() {
		//TODO: if no scores submitted for game, nag.
		fClientFactory.getPlaceController().goTo(new EntryPointPlace());
	}

	public void setGameStateToken(String gameStateToken) {
		fLastGameState = gameStateToken;
		WizGameModel loadGameState = fClientFactory.getGameStateStorage().loadGameState(fLastGameState);
		fGameScorePresenter.displayGameState(loadGameState);
	}

}
